package com.njust.controller;

import com.njust.Util.ExcelUtil;
import com.njust.context.BaseContext;
import com.njust.domain.dto.*;
import com.njust.domain.po.Sample;
import com.njust.domain.vo.PageQueryVO;
import com.njust.domain.vo.PageQueryVO;
import com.njust.domain.vo.PageQueryVO;
import com.njust.domain.vo.SampleVO;
import com.njust.exception.ExcelExistsRepeatedProject;
import com.njust.result.Result;
import com.njust.service.SampleService;
import com.njust.service.TrainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Stream;

/**
 * @version 1.0
 * @Author Imak
 * @Date 2024/11/7 16:53
 * @样品相关接口(法)
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/sample")
@Slf4j
public class SampleController {

    @Autowired
    private SampleService sampleService;

    /**
     * 添加样品
     * @param sampleCreateDTO
     * @return
     */
    @PostMapping("/add_sample")
    public Result<String> AddSample(@RequestBody SampleCreateDTO sampleCreateDTO){
        log.info("添加样品,参数：{}",sampleCreateDTO);
        sampleService.add(sampleCreateDTO);
        return Result.success("添加样品成功");
    }

    /**
     * 导入样品信息
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/import_sample")
    public Result<String> importFromExcel(@RequestParam MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        try(inputStream){
            List<SampleCreateDTO> data = ExcelUtil.readExcel(SampleCreateDTO.class,inputStream, "样品需求清单");
            HashSet<String> set = new HashSet<>();
            sampleService.addList(data);
            log.info("导入项目成功");
        }catch (IOException e){
            log.info("导入异常:{}",e);
        }

        return Result.success("导入项目成功");
    }

    /**
     * 根据id查询样品
     * @param sample_id
     * @return
     */
    @GetMapping("/select_sample/{sample_id}")
    public Result<SampleVO> selectSample(@PathVariable int sample_id){
        log.info("查询样品，id为：{}",sample_id);
        Sample sample=sampleService.selectById(sample_id);
        SampleVO sampleVO=new SampleVO();
        BeanUtils.copyProperties(sample,sampleVO);
        return Result.success(sampleVO);
    }

    /**
     * 修改样品信息
     * @param sampleUpdateDTO
     * @return
     */
    @PutMapping("/update_sample")
    public Result<String> updateSample(@RequestBody SampleUpdateDTO sampleUpdateDTO){
        log.info("修改样品 参数为：{}",sampleUpdateDTO);
        sampleService.update(sampleUpdateDTO);
        return Result.success("修改样品信息成功");
    }

    /**
     * 分页查询样品
     * @param samplePageQueryDTO
     * @return
     */
    @GetMapping("/pageQuerySample")
    public Result<PageQueryVO> pageQuerySample(SamplePageQueryDTO samplePageQueryDTO){
        log.info("样品分页查询，参数为：{}",samplePageQueryDTO);

        PageQueryVO PageQueryVO=sampleService.pageQuerySample(samplePageQueryDTO);
        return Result.success(PageQueryVO);
    }

    /**
     * 分页查询样品需求表
     * @param sampleRequestPageQueryDTO
     * @return
     */
    @GetMapping("/pageQuerySampleRequest")
    public Result<PageQueryVO> pageQuerySampleRequest(SampleRequestPageQueryDTO sampleRequestPageQueryDTO){
        log.info("样品需求分页查询，参数为：{}",sampleRequestPageQueryDTO);

        PageQueryVO pageQueryVO=sampleService.pageQuerySampleRequest(sampleRequestPageQueryDTO);
        return Result.success(pageQueryVO);
    }

    /**
     * 修改样品需求状态
     * @param sampleUpdateStatusDTO
     * @return
     */
    @PostMapping("/update_status")
    public Result<String> updateStatus(@RequestBody SampleUpdateStatusDTO sampleUpdateStatusDTO){
        log.info("修改样品需求状态,{}",sampleUpdateStatusDTO);
        sampleService.updateStatus(sampleUpdateStatusDTO);
        return Result.success("样品需求状态修改成功");

    }

    /**
     * 批量插入样品需求
     * @param sampleRequestCreateDTOList
     * @return
     */
    @PostMapping("/add_request")
    public Result<String> addRequest(@RequestBody List<SampleRequestCreateDTO> sampleRequestCreateDTOList){
        log.info("用户批量添加样品需求,{}",sampleRequestCreateDTOList);
        sampleService.addRequestList(sampleRequestCreateDTOList);
        return Result.success("样品需求添加成功");
    }

    /**
     * 删除样品需求
     * @return
     */
    @DeleteMapping("/delete_request")
    public Result<String> deleteRequest(Integer request_id){
        log.info("删除样品需求,id为: {}",request_id);
        sampleService.deleteRequest(request_id);
        return Result.success("样品需求记录删除成功");
    }


    /**
     * 修改样品需求
     * @param sampleRequestUpdateDTO
     * @return
     */
    @PutMapping("/update_request")
    public Result<String> updateProduct(@RequestBody SampleRequestUpdateDTO sampleRequestUpdateDTO){
        log.info("修改样品需求，{}",sampleRequestUpdateDTO);
        sampleService.updateRequest(sampleRequestUpdateDTO);
        return Result.success("样品需求修改成功");
    }


    /**
     * 导出样品需求清单
     * @param response
     */
    @GetMapping("/export")
    public void exportSampleRequestExcel(HttpServletResponse response){
        sampleService.exportExcel(response);
    }











}
